/*$(document).ready(function(){
	if($("#hidContent").val()!=null&&$("#hidContent").val()!=undefined){
		keditor.html($("#hidContent").val());
	}
	if($("#beginDate").val()!=null&&$("#beginDate").val()!=undefined){
		$("#effect_begin_date").val($("#beginDate").val());
	}
	if($("#endDate").val()!=null&&$("#endDate").val()!=undefined){
		$("#effect_end_date").val($("#endDate").val());
	}
});*/
var KnowledgeEditPage = {
	validate:function($frm, ajax, editor, categoryTree) {
		var text = editor.html();
		var content_str = editor.text();
		var title = $frm.find("input[name='title']").val(); // 标题
		var $endTime = $frm.find("input[name='effect_end_date']");
		var $beginTime = $frm.find("input[name='effect_begin_date']");
		var endTime = $endTime.val(); // 失效时间
		var beginTime = $beginTime.val(); // 生效时间
		if(StringUtil.isBlank(title)){
			MsgBox.warn("标题不能为空", null, null);
			return false;
		}
		if(StringUtil.lengthGreaterThan(title, 32)){
			MsgBox.warn("标题不能太长", null, null);
			return false;
		}
		if(StringUtil.isBlank(beginTime)){
			MsgBox.warn("开始时间不能为空", null, null);
			return false;
		}
		if(StringUtil.isBlank(endTime)){
			MsgBox.warn("结束时间不能为空", null, null);
			return false;
		}
		if(StringUtil.isBlank(text)){
			MsgBox.warn("内容不能为空", null, null);
			return false;
		}
		var sDate = new Date(beginTime.replace(/-/g, "/"));
		var eDate = new Date(endTime.replace(/-/g, "/"));
		var PATTERN= "yyyy-MM-dd HH:mm:ss";
		if(!ValidateUtil.isDatetime(endTime, PATTERN) ||!ValidateUtil.isDatetime(beginTime, PATTERN)){
			MsgBox.warn("请输入正确的时间格式", null, null);
			return false;
		}
		if(sDate > eDate) {
			MsgBox.warn("结束时间不能早于开始时间", null, null);
			return false;
	    }
		$("#knowledgeContentTextarea").val(text);
		$("#content_str").val(content_str);
		var selectedNodes = categoryTree.getSelectedNodes(); // 当前选中的项
		// alert(selectedNodes.length);
		var categoryIds = $.toPropertyValueArray(selectedNodes, 'category_id');
		// Logger.info('categoryIds ==> ' + JSON.stringify(categoryIds));
		var obj = $frm.serializeObject();
		if(categoryIds.length==0){
			MsgBox.warn("请选择问题分类", null, null);
			return false;
		}
		ajax.addParam('categories', categoryIds);
		ajax.addParam('effect_begin_date',sDate);
		ajax.addParam('effect_end_date',eDate);
		delete obj.category_ids;
		
		return true;
	},
	addKnowledge:function(){
		if (!this.validate($("#knowledgeAddFrm"),addKnowledgeAjax, addKnowledgeEditor, categoryXTree)) {
			return ;
		}
		addKnowledgeAjax.send();
	},
	updateKnowledge:function() {
		if (!this.validate($("#knowledgeUpdateFrm"),updateKnowledgeAjax, updateKnowledgeEditor, categoryYTree)) {
			return ;
		}
		updateKnowledgeAjax.send();
	},
	// 知识编辑成功
	onEditSuccess:function(data, status, xhr){
		MsgBox.info(data.message, null, function() {
			questionLstTable.refresh();
			MsgBox.closeActiveDialog();
		});
	},
	categoryId:null,
	// 查询单条记录成功
	onQueryOneSuccess:function(data, status, xhr){
		if (data.info.categoryList) {
			data.info.category_ids = $.toPropertyValueArray(data.info.categoryList, 'category_id')[0];
		}
		// 查询知识分类的ajax与查询单条知识记录的ajax无法获知调用success的先后，故两边同时做操作
		KnowledgeEditPage.categoryId = data.info.category_ids;
		categoryYTree && categoryYTree.selectNodeById(KnowledgeEditPage.categoryId);
		
		$("#knowledgeUpdateFrm").autofill(data.info);
		data.info.content && updateKnowledgeEditor.html(data.info.content);
	},
	// 新增知识页面查询所有知识分类成功的回调
	onQueryCategoryXSuccess:function(data, status, xhr){
		categoryXTree.expandAll();
		categoryXTree.selectFirstRootNode();
	},
	// 修改知识页面查询所有知识分类成功的回调
	onQueryCategoryYSuccess:function(data, status, xhr){
		categoryYTree.expandAll();
		// 查询知识分类的ajax与查询单条知识记录的ajax无法获知调用success的先后，故两边同时做操作
		KnowledgeEditPage.categoryId && categoryYTree.selectNodeById(KnowledgeEditPage.categoryId);
	}
};